using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author: XiaoChunhu
    /// </summary>
    public partial class CleanScoreEntry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140813";//set the Menu
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);//use the function
        }

        #endregion

        #region page load and set the page lanuage

        //when the page load,these funtion will be run
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;//allow to chanage the page
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
                this.btnNew.Visible = false;
            }

        }

        //when you chanage the language,this function will be run
        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
            if (IsPostBack) //added by Huangyao 20090119
            {
                SetPageLoad();
            }
        }

        //get the word for the page and by the lanuage
        private void SetLanguage()
        {
            this.lblTitle.Text = this.GetLabel("SOM_LineEntryTitle");
            this.lblLine.Text = this.GetLabel("SOM_LineEntryLine", true);
            this.lblPassScore.Text = this.GetLabel("SOM_LineEntryPassScore", true);
            this.lblVoid.Text = this.GetLabel("SOM_Void", true);
            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnNew.Text = this.GetLabel("SOM_ShiftCodeEntryAddNew");
            this.btnSaveReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");
            BindLine();//when the page load, this funtion will to bind some data on the Line
        }

        /// <summary>
        /// at the pageload this function will run, and if nothing send here,it will load a new page and if something send here it will bind some data on the control by the CleanEstablishmentID
        /// </summary>
        private void SetPageLoad()
        {
            if (this.Request.QueryString["CleanPassScoreid"] != null)//is something send here 
            {
                txtSession.Text = this.Request.QueryString["CleanPassScoreid"].Trim();
                //get datatable by the CleanPassScoreid
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDLine.GetLineScoreByKey(int.Parse(this.Request.QueryString["CleanPassScoreid"]));
                if (bool.Parse(dt.Rows[0]["FlagObsolete"].ToString()))
                {
                    ddlLine.SelectedValue = dt.Rows[0]["LINEid"].ToString();//bind LineCode on the control
                    txtPassScore.Text = dt.Rows[0]["PassScore"].ToString();//bind PassScore on the control
                    chVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());//bind FlagObsolete on the control
                    ddlLine.Enabled = false;
                    txtPassScore.Enabled = false;
                    chVoid.Enabled = false;
                    btnSave.Enabled = false;
                    btnSaveNew.Enabled = false;
                    btnSaveReturn.Enabled = false;
                }
                else
                {
                    ddlLine.SelectedValue = dt.Rows[0]["LINEid"].ToString();//bind LineCode on the control
                    txtPassScore.Text = dt.Rows[0]["PassScore"].ToString();//bind PassScore on the control
                    chVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());//bind FlagObsolete on the control
                    ddlLine.Enabled = false;
                }
            }
            //add the message on the button,when you click the button,it will to check the control and show the message
            this.btnSave.Attributes.Add("onclick", "return CheckData('" +
                                                                            this.GetMessage("SOM_LineScoreEntryLine") + "','" +
                                                                            this.GetMessage("SOM_LineScoreEntryPassScore") + "')");
            this.btnSaveNew.Attributes.Add("onclick", "return CheckData('" +
                                                                        this.GetMessage("SOM_LineScoreEntryLine") + "','" +
                                                                        this.GetMessage("SOM_LineScoreEntryPassScore") + "')");
            this.btnSaveReturn.Attributes.Add("onclick", "return CheckData('" +
                                                                        this.GetMessage("SOM_LineScoreEntryLine") + "','" +
                                                                        this.GetMessage("SOM_LineScoreEntryPassScore") + "')");
        }

        #endregion

        #region Bind some data

        //get the Line to bind on the control
        private void BindLine()
        {
            //get the datatable by the LangCode
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDLine.GetActive(this.LangCode);
            BindListControl(this.ddlLine, "LineID", "Code", dt, true,"");//bind some data on the control
            dt.Dispose();
            dt = null;
        }

        //if there are some data on the control, use the function can clear these data
        private void Clear()
        {
            //set all control's Enabled is true
            ddlLine.Enabled = true;
            txtPassScore.Enabled = true;
            chVoid.Enabled = true;
            //clear all data
            BindLine();
            txtPassScore.Text = "";
            chVoid.Checked = false;
            txtSession.Text = "";
        }

        //use this function can do insert or update
        private void Save(int CleanPassScoreid, int action)
        {
            try
            {
                //to do insert or update by action,if action is 1,it will do insert,if action is 2,it will do update
                MTRC.ODMS.DAL.SOM.CodeSetup.CDLine.UpdateLineScore(CleanPassScoreid,
                    int.Parse(ddlLine.SelectedValue),
                    int.Parse(txtPassScore.Text),
                    chVoid.Checked,
                    this.ODMUser.ID,
                    MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime(),
                    this.ODMUser.ID,
                    action);
                this.Alert(this.GetMessage("SOM_LineScoreSuccessful"));
                AfterVoid();
            }
            catch
            {
                this.Alert(this.GetMessage("SOM_LineScoreFailing"));
            }
        }

        private void AfterVoid()
        {
            if (chVoid.Checked)
            {
                ddlLine.Enabled = false;
                txtPassScore.Enabled = false;
                chVoid.Enabled = false;
                btnSave.Enabled = false;
                btnSaveNew.Enabled = false;
                btnSaveReturn.Enabled = false;
            }
        }

        //this function will to check you wang to insert data exist
        private bool CheckUnique(bool insert)
        {
            //get the Count,if the Count more than 0, you wang to insert data is exist,you can't to do insert
            int DataCount = MTRC.ODMS.DAL.SOM.CodeSetup.CDLine.GetMessageToInsert(int.Parse(this.ddlLine.SelectedValue)).Rows.Count;
            if (DataCount > 0)
            {
                this.Alert(string.Format(this.GetMessage("SOM_HaveSomeData"), this.ddlLine.SelectedValue, this.txtPassScore.Text));
                return false;
            }
            return true;
        }

        private bool CheckData()
        {
            //check station: must exists in table SOM_CDStation
            //update by gordon 20151019  U247 Can not Test 
            //if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.StationID))
            if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.LocationID))
            {
                //this.Alert("Current station is not an legal station for SOM");
                this.Alert(this.GetMessage("SOM_LegalStationCheck"));
                return false;
            }
            return true;
        }

        #endregion

        #region some envet

        //when you want to create a new data,and there are some data on the page,you can click the "new"
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Clear();
        }

        //when you write some data on the page,and you want to save these data,you can click the "Save"
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
            if (txtSession.Text != "")
            {
                Save(int.Parse(txtSession.Text), 2);
            }
            else
            {
                //to use a function to do the check the data exist
                if (CheckUnique(true))
                {
                    Save(0, 1);
                }
            }
        }

        //when you write some data on the page,and you want to save these data and return the last page,you can click the "Save And Return"
        protected void btnSaveReturn_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
            if (txtSession.Text!="")
            {
                Save(int.Parse(txtSession.Text), 2);
                //to return the last page
                string script = "<script language=javascript>function btnChange_Click(){window.location.href='CleanScoreList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                this.Page.RegisterStartupScript("aaa", script);
            }
            else
            {
                //to use a function to do the check
                if (CheckUnique(true))
                {
                    Save(0, 1);
                    //to return the last page
                    string script = "<script language=javascript>function btnChange_Click(){window.location.href='CleanScoreList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                    this.Page.RegisterStartupScript("aaa", script);
                }
            }
            
        }

        //when you write some data on the page,and you want to save these data and you have other data want to insert,you can click the "Save And New"
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
            if (txtSession.Text!="")
            {
                Save(int.Parse(txtSession.Text), 2);
            }
            else
            {
                //to use a function to do the check
                if (CheckUnique(true))
                {
                    Save(0, 1);
                }
            }
            //to clear the data on the page to insert other data
            Clear();
        }

        //if you want to do nothing,and want to return the last page,you can click the "Cancel"
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //return the last page
            this.Response.Redirect("CleanScoreList.aspx");
        }

        protected void chVoid_CheckedChanged(object sender, EventArgs e)
        {
            if (chVoid.Checked)
            {
                Alert(this.GetMessage("SOM_MsgVoid"));
            }
        }

        #endregion

        
    }
}
